<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <!-- <a-form-item label="processId" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-input v-decorator="[ 'processId', validatorRules.processId]" placeholder="请输入processId"></a-input>
         </a-form-item>-->
        <a-form-item label="流程类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag :disabled="isEdit" type="list" v-decorator="['approvalProcessType']" :trigger-change="true"
                             dictCode="approval_process_type" placeholder="请选择流程类型"/>
        </a-form-item>

        <a-form-item label="流程节点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!--          <a-input v-decorator="[ 'approvalProcessCode', validatorRules.approvalProcessCode]" placeholder="请输入approvalProcessCode"></a-input>-->
          <j-dict-select-tag :disabled="isEdit" type="list" v-decorator="['approvalProcessCode']" :trigger-change="true"
                             dictCode="current_status_pkh" placeholder="请选择流程节点"/>
        </a-form-item>

        <!-- <a-form-item label="受理时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <j-date placeholder="请选择受理时间" v-decorator="[ 'approvalProcessDate', validatorRules.approvalProcessDate]" :trigger-change="true" style="width: 100%"/>
         </a-form-item>-->

        <a-form-item label="受理人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input  v-decorator="[ 'operator', validatorRules.operator]"
                   placeholder="请输入受理人"></a-input>
        </a-form-item>

        <a-form-item label="回复内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea  v-decorator="['replyContent',validatorRules.replyContent]" rows="4" placeholder="请输入回复内容"/>
        </a-form-item>

        <a-form-item label="相关资料" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload-img v-decorator="['information', validatorRules.information]" :trigger-change="true"></j-upload-img>
        </a-form-item>

        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag :disabled="isEditState" type="list" v-decorator="['state',validatorRules.state]" :trigger-change="true"
                             dictCode="process_state" placeholder="请选择状态"/>
        </a-form-item>
        <div style="text-align: center" v-if="isShowGongShi">
          <a @click="gongShi()">
            <a-button type="primary" icon="plus">添加数据到公示台账</a-button>
          </a>
        </div>
        <div style="text-align: center" v-if="isShowTuiJian">
          <a @click="tuiJian()">
            <a-button type="primary" icon="plus">添加数据到推荐台账</a-button>
          </a>
        </div>
        <div style="text-align: center" v-if="isShowFangKuan">
          <a @click="fangKuan()">
            <a-button type="primary" icon="plus">添加数据到放款台账</a-button>
          </a>
        </div>
      </a-form>
    </a-spin>
    <SmallAmountPublicityAccountModal ref="smallAmountPublicityAccountModal"></SmallAmountPublicityAccountModal>
    <SmallLoanAccountModal ref="smallLoanAccountModal"></SmallLoanAccountModal>
    <SmallRecommendationAccountModal ref="smallRecommendationAccountModal"></SmallRecommendationAccountModal>
  </a-modal>
</template>

<script>

    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'
    import JUpload from '@/components/jeecg/JUpload'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"
    import SmallAmountPublicityAccountModal from "../SmallAmountPublicityAccountModal";
    import SmallLoanAccountModal from "../SmallLoanAccountModal";
    import SmallRecommendationAccountModal from "../SmallRecommendationAccountModal";
    import JUploadImg from "../../../../../components/jeecg/JUploadImg";

    export default {
        name: "ProcessFinishModal1",
        components: {
            JUploadImg,
            JDate,
            JUpload,
            JDictSelectTag,
            SmallRecommendationAccountModal,
            SmallLoanAccountModal,
            SmallAmountPublicityAccountModal,
        },
        data() {
            return {
                state:'',
                isShowGongShi: false,
                isShowTuiJian: false,
                isShowFangKuan: false,
                isEdit: true,
                isEditState:true,
                form: this.$form.createForm(this),
                title: "操作456",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                validatorRules: {
                    processId: {},
                    approvalProcessType: {},
                    approvalProcessCode: {},
                    approvalProcessDate: {},
                    information: {},
                    operator: {rules: [{ required: true, message: '受理人不能为空'}]},
                    replyContent: {rules: [{ required: true, message: '回复内容不能为空'}]},
                    state: {rules: [{ required: true, message: '状态不能为空'}]},
                },
                url: {
                    add: "/loanmanagement/processFinish/add",
                    edit: "/loanmanagement/processFinish/edit",
                }

            }
        },
        created() {
        },
        methods: {
            gongShi() {
                var processId= this.model.processId;
                this.$refs.smallAmountPublicityAccountModal.showGoodsGallery1(processId);
            },
            tuiJian() {
                var processId= this.model.processId;
                this.$refs.smallRecommendationAccountModal.showGoodsGallery1(processId);
            },
            fangKuan() {
                var processId= this.model.processId;
                this.$refs.smallLoanAccountModal.showGoodsGallery1(processId);
            },
            add() {
                this.edit({});
            },
            setTitle(res) {
                if (res.approvalProcessCode != 'false') {
                    if (res.approvalProcessCode == '3') {
                        this.isShowGongShi = true;
                        this.isShowTuiJian = false;
                        this.isShowFangKuan = false;
                    } else if (res.approvalProcessCode == '4') {
                        this.isShowGongShi = false;
                        this.isShowTuiJian = true;
                        this.isShowFangKuan = false;
                    } else if (res.approvalProcessCode == '7') {
                        this.isShowGongShi = false;
                        this.isShowTuiJian = false;
                        this.isShowFangKuan = true;
                    }else{
                        this.isShowGongShi = false;
                        this.isShowTuiJian = false;
                        this.isShowFangKuan = false;
                    }
                }
            },
            edit(record) {
                this.state=record.state;
                this.setTitle(record);
                //如果状态由  拒绝转换为通过，则数据由finish表转移到ing表
                if (record.state == '1') {
                    this.isEditState = false;
                } else {
                    this.isEditState = true;
                }
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'processId', 'approvalProcessType', 'approvalProcessCode', 'approvalProcessDate', 'operator', 'replyContent', 'information', 'state'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if(!this.model.id){
                            httpurl+=this.url.add;
                            method = 'post';
                        }else{
                            httpurl+=this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据",formData)
                        httpAction(httpurl,formData,method).then((res)=>{
                            if(res.success){
                                that.$message.success(res.message);
                                that.$emit('ok');
                            }else{
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                            if (formData.state != this.state) {
                                this.$emit('parentComponentClose', 'close');
                            }
                        })
                    }

                })
                this.close();
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'processId', 'approvalProcessType', 'approvalProcessCode', 'approvalProcessDate', 'operator', 'replyContent', 'information', 'state'))
            }

        }
    }
</script>